package com.aiwei.tdjk.controller;

import com.aiwei.tdjk.bean.Pager;
import com.aiwei.tdjk.common.Constants;
import com.aiwei.tdjk.dto.request.CustomsHandbookProductdetailPagerRequest;
import com.aiwei.tdjk.dto.response.CustomsHandbookProductdetailResponse;
import com.aiwei.tdjk.entity.CustomsHandbookProductdetail;
import com.aiwei.tdjk.service.CustomsHandbookProductdetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;

@Controller
@RequestMapping(value = "/admin/customsHandbookProductdetail")
public class CustomsHandbookProductdetailController extends BaseController {

	@Autowired
	private CustomsHandbookProductdetailService customsHandbookProductdetailService;


	@RequestMapping(value = {"/list"}, method = RequestMethod.GET)
	public String list(CustomsHandbookProductdetailPagerRequest request, Model model) {
        Pager<CustomsHandbookProductdetailResponse> pager = customsHandbookProductdetailService.find(request);
        model.addAttribute("page", pager);
		model.addAttribute("customsHandbookProductdetail", request);
		return viewName("list");
	}

    @RequestMapping(value = "create", method = RequestMethod.GET)
    public String showCreateForm(Model model,Long customsHandBookId) {
        model.addAttribute(Constants.OP_NAME, "新增");
        if (!model.containsAttribute("m")) {
		    CustomsHandbookProductdetail m = new CustomsHandbookProductdetail();
		    m.setCustomsHandBookId(customsHandBookId);
            model.addAttribute("m", m);
        }
        return viewName("editForm");
	}

    @RequestMapping(value = "create", method = RequestMethod.POST)
    public String create(
            Model model, @Valid @ModelAttribute("m") CustomsHandbookProductdetail m, BindingResult result,
            RedirectAttributes redirectAttributes) {
        if (hasError(result)) {
            return showCreateForm(model,m.getCustomsHandBookId());
        }
		customsHandbookProductdetailService.add(m);
        redirectAttributes.addFlashAttribute(Constants.MESSAGE, "新增成功");
        return redirectToUrl("list?customsHandBookId="+m.getCustomsHandBookId());
	}

	@RequestMapping(value = "{id}/update", method = RequestMethod.GET)
	public String showUpdateForm(@PathVariable("id") Long id, Model model) {
        model.addAttribute(Constants.OP_NAME, "修改");
        model.addAttribute("m", customsHandbookProductdetailService.get(id));
        return viewName("editForm");
	}

	@RequestMapping(value = "{id}/update", method = RequestMethod.POST)
	public String update(
        Model model, @Valid @ModelAttribute("m") CustomsHandbookProductdetail m, BindingResult result,
        RedirectAttributes redirectAttributes) {
        if (hasError(result)) {
        return showUpdateForm(m.getId(), model);
        }
		customsHandbookProductdetailService.update(m);
        redirectAttributes.addFlashAttribute(Constants.MESSAGE, "修改成功");
        return redirectToUrl("list?customsHandBookId="+m.getCustomsHandBookId());
	}


    @RequestMapping(value = "{id}/delete", method = RequestMethod.POST)
    public String delete(@PathVariable("id") Long id,
            RedirectAttributes redirectAttributes) {

        customsHandbookProductdetailService.delete(id);
        redirectAttributes.addFlashAttribute(Constants.MESSAGE, "删除成功");
        return redirectToUrl("list");
    }

    @RequestMapping(value = "batch/delete", method = {RequestMethod.GET, RequestMethod.POST})
    public String deleteInBatch(@RequestParam(value = "ids", required = false) Long[] ids,
            RedirectAttributes redirectAttributes) {

        customsHandbookProductdetailService.delete(ids);
        redirectAttributes.addFlashAttribute(Constants.MESSAGE, "删除成功");
        return redirectToUrl("list");
    }


}
